$(document).ready(init);

var atNum = 0;
var attentionNum = 0;
var commentNum = 0;
var feedNum = 0;
var messageNum = 0;

function init() {
	setInterval(checkNewMessage, 15000);
}

function checkNewMessage() {
	var requestUrl = 'http://t.qiyi.com/api/common/new_msg.php';

	$.get(requestUrl, '', newMessageCallback, 'json');
}

function newMessageCallback(data) {
	var hasBadge = false;
	if (data.code != 'A00000') {
		return ;
	}

	if (data.data.atme != 0 || data.data.attention != 0 || data.data.comment != 0 || data.data.feed != 0 || data.data.msg != 0) {
		hasBadge = true;
	}

		atmeNum = data.data.atme;
		commentNum = data.data.comment;
		feedNum = data.data.feed;
		messageNum = data.data.msg;

	if (hasBadge) {
		chrome.browserAction.setBadgeText({text: '!'});
	} else {
		chrome.browserAction.setBadgeText({text: ''});
	}
}

// Array to hold callback functions
var callbacks = []; 

// This function is called onload in the popup code
function getPageInfo(callback) 
{ 
    // Add the callback to the queue
    callbacks.push(callback); 

    // Injects the content script into the current page 
    chrome.tabs.executeScript(null, { file: "content_script.js" }); 
}; 

// Perform the callback when a request is received from the content script
chrome.extension.onRequest.addListener(function(request) 
{ 
    // Get the first callback in the callbacks array
    // and remove it from the array
    var callback = callbacks.shift();

    // Call the callback function
    callback(request); 
}); 